#include "grate_test_utils.h"
#include "grate/util/word.h"

using growth_rate::Word;

namespace {

TEST(Word, IsSquare) {
  EXPECT_TRUE(Word("").is_square());
  EXPECT_FALSE(Word("aba").is_square());
  EXPECT_TRUE(Word("abcbabcb").is_square());
}

TEST(Word, IsCube) {
  EXPECT_TRUE(Word("").is_cube());
  EXPECT_TRUE(Word("abcabcabc").is_cube());
  EXPECT_FALSE(Word("abcabdabc").is_cube());
  EXPECT_FALSE(Word("abcabcab").is_cube());
}

TEST(Word, AppendTest) {
  uint8_t buffer[20] = {0};
  Word word = Word::createWordFromArray(buffer, 0);
  EXPECT_EQ(0, word.length());
  word = word.append("aba");
  word = word.append("c");
  EXPECT_EQ("abac", word.str());
  word = word.append(word);
  EXPECT_EQ("abacabac", word.str());
  word = word.append('a');
  EXPECT_EQ("abacabaca", word.str());
}

TEST(Word, PrefixAndSuffix) {
  Word word("abcdef");
  EXPECT_EQ("", word.prefix(0).str());
  EXPECT_EQ("abcde", word.prefix(5).str());
  EXPECT_EQ("abcdef", word.prefix(6).str());
  EXPECT_EQ("", word.suffix(0).str());
  EXPECT_EQ("cdef", word.suffix(4).str());
  EXPECT_EQ("bcdef", word.drop_first(1).str());
  EXPECT_EQ("", word.drop_first(6).str());
  EXPECT_EQ("bcd", word.substr(1, 3).str());
  EXPECT_EQ("abcdef", word.substr(0, 6).str());
  EXPECT_EQ("", word.substr(2, 0).str());
}

TEST(Word, ComparisonOperators) {
  uint8_t array[4] = {0, 1, 2, 0};
  Word array_word = Word::createWordFromArray(array, 4);
  EXPECT_TRUE(array_word == Word("abca"));
  EXPECT_FALSE(array_word != Word("abca"));
  EXPECT_TRUE(array_word != Word("abc"));
  EXPECT_FALSE(array_word == Word("abc"));
  EXPECT_TRUE(array_word.prefix(1) == array_word.suffix(1));
}

}  // namespace
